<template>
  <div class="app-container">
    <el-table
      :data="
        tableData.slice((currentPage - 1) * PageSize, currentPage * PageSize)
      "
      border
      style="width: 100%"
    >
      <el-table-column prop="_id" label="用户id"> </el-table-column>
      <el-table-column prop="userName" label="用户名"> </el-table-column>
      <el-table-column prop="password" label="密码"> </el-table-column>
      <!-- <el-table-column prop="" label="地址"> </el-table-column> -->
      <el-table-column prop="avatar" label="头像"
        ><template slot-scope="scope">
          <!-- 作用域插槽 -->
          <img
            :src="scope.row.avatar | handleImgPath"
            width="100"
            height="100"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <!-- scope.row 获取当前这条数据   @click="delUser(scope.row)"-->
          <el-button @click="editUser(scope.row)" type="primary" size="small"
            >修改</el-button
          >
          <!-- <el-button type="danger" size="small">删除</el-button> -->
          <el-popconfirm
            title="确定删除这条用户信息吗？"
            @onConfirm="delUser(scope.row)"
          >
            <el-button type="danger" size="small" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改用户信息 -->
    <el-dialog title="修改用户信息" :visible.sync="dialogVisible" width="60%">
      <el-form ref="user" :model="user" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="http://lanlianhua.work:8888/api/v1/common/file_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next,jumper"
      :total="totalCount"
      :page-size="PageSize"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getUsers, xiugaiUser, deleteUser, getAddress } from "../../api/user";
export default {
  name: "UserList",
  components: {},
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      imageUrl: "",
      id: "",
      totalCount: 0,
      currentPage: 1,
      PageSize: 10,
      user: {},
    };
  },
  computed: {},
  filters: {
    //过滤头像
    handleImgPath(str) {
      if (str) {
        if (str.startsWith("http") || str.startsWith("data")) {
          return str;
        } else {
          return "http://lanlianhua.work:8888" + str;
        }
      } else {
        return "http://www.superslide2.com/demo/images/pic3.jpg";
      }
    },
  },
  created() {
    this.initUsers();
    // this.initAddress();
  },
  methods: {
    //获取用户列表
    async initUsers() {
      const result = await getUsers(350, 1);
      // console.log(111, result);
      this.tableData = result.users;
      console.log(this.tableData);
      this.totalCount = result.totalCount;
    },

    //分页
    handleCurrentChange(val) {
      this.currentPage = val;
    },

    //获取用户地址
    // async initAddress() {
    //   console.log(this.id);
    //   // const result = await getAddress(this.id);
    //   // console.log(222, result);
    // },

    //上传头像
    handleAvatarSuccess(res, file) {
      console.log(res);
      console.log(file);
      this.imageUrl = "http://lanlianhua.work:8888" + res.info;
    },
    //过滤头像
    handleImgPath(str) {
      if (str) {
        if (str.startsWith("http") || str.startsWith("data")) {
          return str;
        } else {
          return "http://lanlianhua.work:8888" + str;
        }
      } else {
        return "http://www.superslide2.com/demo/images/pic3.jpg";
      }
    },
    //弹出修改弹窗
    editUser(user) {
      this.dialogVisible = true; //打开弹框
      //数据回显
      this.user.name = user.userName;
      this.imageUrl = this.handleImgPath(user.avatar);
      this.id = user._id;
    },
    //确定修改
    async editSure() {
      const result = await xiugaiUser(this.id, {
        userName: this.user.name,
        avatar: this.imageUrl,
      });
      console.log(222, result);
      this.dialogVisible = false; // 关闭弹框
      this.initUsers(); // 再次调用接口
    },
    //删除
    async delUser(user) {
      console.log(user);
      const result = await deleteUser(user._id);
      if (result.code === 20000) {
        console.log("删除成功", result);
        this.initUsers(); // 再次调用接口
      } else {
        console.log(result.error);
      }
    },
    //判断上传的文件是否符合下面的规范
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
